//
//  UserManager.m
//  安好
//
//  Created by 冯凯 on 14-10-30.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "UserManager.h"
#import "UserModel.h"

@implementation UserManager

+ (UserManager *)shareInstance {
    static UserManager *userManager;
    if (userManager == nil) {
        userManager = [[UserManager alloc] init];
    }
    return userManager;
}
#pragma - mark 获取用户是否登录
/**
 *  获取用户的登录状态
 *
 *  @return 无
 */
- (NSString *)getUserIsLogin
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    if ([user objectForKey:@"SelfUidKey"] == nil){
        return @"";
    }else{
    NSString * userIsLogin = [NSString stringWithFormat:@"%@",[user objectForKey:@"SelfUidKey"]];
        return userIsLogin;
    }
}
#pragma - mark 获取用户的资料
/**
 *  获取用户的资料
 *
 *  @return 获取用户的资料
 */
- (UserModel *)getUserInfo
{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSString * uidKey = [userDefaults objectForKey:@"SelfUidKey"];
    NSString * userInfoKey = [NSString stringWithFormat:@"%@userInfoKey",uidKey];
    NSMutableArray * userInfo = [userDefaults rm_customObjectForKey:userInfoKey];
    UserModel * user =[userInfo objectAtIndex:0];
    return user;
}
#pragma - mark 用户登录
/**
 *  用户的登录
 *
 *  @param userName 用户名
 *  @param passWord 密码
 */
- (void)Login:(NSString *)userName :(NSString *)passWord
{
    NSString * postURL = [NSString stringWithFormat:@"%@user/login/",kBaseURL];
    NSDictionary *LoginDic = @{
                               @"username":userName,
                               @"password":passWord,
                               @"deviceid":@"",
                               };
    NSString * userString = [LoginDic JSONString];
    NSString * encryptUserString = [ClassMethodsViewController encrypt:userString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"platform": @"ios",@"version":@"1.1",@"user":encryptUserString};
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            NSDictionary * rooms = [RMMapper objectWithClass:[UserModel class] fromDictionary:[responseObject objectForKey:@"data"]];
            NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
            NSDictionary * dataDic = [responseObject objectForKey:@"data"];
            NSString * uidKey = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"uid"]];
            [[NSUserDefaults standardUserDefaults] setObject:uidKey forKey:@"SelfUidKey"];
            NSString * userInfoKey = [NSString stringWithFormat:@"%@userInfoKey",uidKey];
            NSMutableArray * userInfo = [[NSMutableArray alloc]init];
            [userInfo addObject:rooms];
            [defaults rm_setCustomObject:userInfo forKey:userInfoKey];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Login" object:nil];

            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"Error: %@", error);
    }];
}

#pragma - mark 取消登录
/**
 *  取消登录
 */
- (void)logout
{
    NSString * postURL = [NSString stringWithFormat:@"%@user/logout/",kBaseURL];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"platform": @"ios",@"version":@"1.1"};
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"Error: %@", error);
    }];
}
#pragma - mark 用户注册
/**
 *  用户注册
 *
 *  @param userName 用户的昵称
 *  @param email    用户的邮箱
 *  @param passWord 用户的密码
 */
- (void)registered:(NSString *)userName :(NSString *)email :(NSString *)passWord
{
    NSString * postURL = [NSString stringWithFormat:@"%@user/register/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"platform": @"ios",@"version":@"1.1"};
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"Error: %@", error);
    }];
}
/**
 *  判断是否写入图片
 *
 *  @return 无
 */
- (BOOL)isWriteImage
{
    UserModel * usermodel = [[UserManager shareInstance]getUserInfo];
    UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:usermodel.avatar]]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString * uid = usermodel.uid;
    NSString * imageName = [NSString stringWithFormat:@"%@.png",uid];
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
//        NSLog(@"imagePath:%@",filePath);
        // 保存文件的名称
    BOOL result = [UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];
        
    return result;
}
/**
 *  判断是否存在图片
 *
 *  @return 无
 */
- (BOOL)isHaveUserImage
{
    UserModel * usermodel = [[UserManager shareInstance]getUserInfo];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString * uid = usermodel.uid;
    NSString * imageName = [NSString stringWithFormat:@"/%@.png",uid];
    NSString *path = [[paths objectAtIndex:0]stringByAppendingString:imageName ];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:path];

    return result;
}
/**
 *  获取沙盒的图片
 *
 *  @return 无
 */
- (UIImage *)getUserImage
{
    UserModel * usermodel = [[UserManager shareInstance]getUserInfo];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString * uid = usermodel.uid;
    NSString * imageName = [NSString stringWithFormat:@"/%@.png",uid];
    NSString *path = [[paths objectAtIndex:0]stringByAppendingString:imageName ];
    UIImage * userImage = [UIImage imageWithContentsOfFile:path];
    return userImage;
}
/**
 *  删除沙盒的图片
 */
- (void)deleteUserImage
{
    UserModel * usermodel = [[UserManager shareInstance]getUserInfo];
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString * uid = usermodel.uid;
    NSString * imageName = [NSString stringWithFormat:@"/%@.png",uid];
    //文件名
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingString:imageName];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
    }
}

@end
